/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package Bean;

import db.getDBConnection;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.naming.spi.DirStateFactory;

/**
 *
 * @author 1Stmax
 */
public class location {

    private int locationid;
    private String countryName;
    private String address;
    private String cityName;
    private float cpermitPrice;
    private float opermitPrice;
    private float stampdutyPrice;
    private boolean LocationStatus;
    private float PricePerFt;

    public location() {
    }

    public int getLocationid() {
        return locationid;
    }

    public void setLocationid(int locationid) {
        this.locationid = locationid;
    }

    public String getCountryName() {
        return countryName;
    }

    public void setCountryName(String countryName) {
        this.countryName = countryName;
    }

    public String getCityName() {
        return cityName;
    }

    public void setCityName(String cityName) {
        this.cityName = cityName;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public float getCpermitPrice() {
        return cpermitPrice;
    }

    public void setCpermitPrice(float cpermitPrice) {
        this.cpermitPrice = cpermitPrice;
    }

    public float getOpermitPrice() {
        return opermitPrice;
    }

    public void setOpermitPrice(float opermitPrice) {
        this.opermitPrice = opermitPrice;
    }

    public float getStampdutyPrice() {
        return stampdutyPrice;
    }

    public void setStampdutyPrice(float stampdutyPrice) {
        this.stampdutyPrice = stampdutyPrice;
    }

    public boolean isLocationStatus() {
        return LocationStatus;
    }

    public void setLocationStatus(boolean LocationStatus) {
        this.LocationStatus = LocationStatus;
    }

    public float getPricePerFt() {
        return PricePerFt;
    }

    public void setPricePerFt(float PricePerFt) {
        this.PricePerFt = PricePerFt;
    }

    public Vector<location> getAllLocations() {
        Connection con = null;
        Statement stmt = null;
        ResultSet rs = null;
        Vector<location> v = new Vector();
        try {
            String sql = "SELECT * FROM Locations";
            con = db.getDBConnection.getConnection();
            stmt = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY);
            rs = stmt.executeQuery(sql);
            while (rs.next()) {
                location l = new location();
                l.setLocationid(rs.getInt("LocationID"));
                l.setCountryName(rs.getString("CountryName"));
                l.setAddress(rs.getString("Address"));
                l.setCityName(rs.getString("CityName"));
                l.setCpermitPrice(rs.getFloat("CPermitPrice"));
                l.setPricePerFt(rs.getFloat("OPermitPrice"));
                l.setStampdutyPrice(rs.getFloat("StampDutyPrice"));
                l.setLocationStatus(rs.getBoolean("LocationStatus"));
                l.setPricePerFt(rs.getFloat("PricePerFt"));
                v.add(l);
            }
        } catch (SQLException ex) {
            Logger.getLogger(location.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            try {
                con.close();
            } catch (SQLException ex) {
                Logger.getLogger(location.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        return v;
    }

    public String insertLocations(String CountryName, String Address, String CityName, float CPermitPrice, float OPermitPrice, float StampDutyPrice, boolean LocationStatus, float PricePerFt) {
        Connection con = null;
        Statement stmt = null;
        ResultSet rs = null;
        String sqlInsert = "INSERT INTO Locations VALUES(" + CountryName + "," + CityName + "," + CPermitPrice + ","
                + OPermitPrice + "," + LocationStatus + "," + PricePerFt + ");";
        try {
            con = db.getDBConnection.getConnection();
            stmt = con.createStatement();
            rs = stmt.executeQuery(sqlInsert);
        } catch (SQLException ex) {
            Logger.getLogger(location.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            try {
                con.close();
            } catch (SQLException ex) {
                Logger.getLogger(location.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        return "success";
    }

    public String updateLocations(String CountryName, String Address, String CityName, float CPermitPrice, float OPermitPrice, float StampDutyPrice, boolean LocationStatus, float PricePerFt) {
        Connection con = null;
        PreparedStatement stmt = null;
        ResultSet rs = null;
        String sqlUpdate = "UPDATE Locations SET CountryName=?,Address=?,CityName=?,CPermitPrice=?,OPermitPrice=?,StampDutyPrice=?,LocationStatus=?,PricePerFt=?";
        try {
            con.setAutoCommit(false);
            con = getDBConnection.getConnection();
            con.createStatement();
            stmt = con.prepareStatement(sqlUpdate);
            stmt.setString(1, CountryName);
            stmt.setString(2, Address);
            stmt.setString(3, CityName);
            stmt.setFloat(4, CPermitPrice);
            stmt.setFloat(5, OPermitPrice);
            stmt.setFloat(6, StampDutyPrice);
            stmt.setBoolean(7, LocationStatus);
            stmt.setFloat(8, PricePerFt);
            stmt.executeUpdate();
            con.commit();
        } catch (SQLException ex) {
            if (con != null) {
                try {
                    con.rollback();
                } catch (SQLException ex1) {
                    try {
                        Logger.getLogger(location.class.getName()).log(Level.SEVERE, null, ex1);
                        con.rollback();
                    } catch (SQLException ex2) {
                        Logger.getLogger(location.class.getName()).log(Level.SEVERE, null, ex2);
                    }
                }
            }
            Logger.getLogger(location.class.getName()).log(Level.SEVERE, null, ex);

        } finally {
            try {
                con.setAutoCommit(true);
                con.close();
            } catch (SQLException ex) {
                Logger.getLogger(location.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        return "success";
    }
}
